class Organization < ActiveRecord::Base
	belongs_to :user

	has_one :org_logo
	has_one :org_photo
	
	has_many :people
		
	has_and_belongs_to_many :countries
	
	has_many :take_parts
	has_many :activities, :through => :take_parts
	
	after_update :save_people
		
	attr_writer :other_activity		
	
	def logo_attributes=(logo_attributes)
		create_org_logo(logo_attributes)
	end
	
	def photo_attributes=(photo_attributes)
		create_org_photo(photo_attributes)
	end
	
	
	def person_attributes=(person_attributes)
		person_attributes.each do |attributes|
			if attributes[:id].blank?
				people.build(attributes)
			else
				person = people.detect { |p| p.id == attributes[:id].to_i }
				person.attributes = attributes
			end
		end
	end
	
	
	def save_people
		people.each do |p|
			p.save(false)
		end
	end
	
	
	def other_activity
		rv = nil
		self.take_parts.each do |tp|
			rv = tp.description if tp.activity_id == 9
		end
		return rv.nil? ? "" : rv
	end
	
	
	def activity_ids
		self.activities.map(&:id)
	end
   
	def activity_ids=(new_ids)
		ids = (new_ids || []).reject(&:blank?)
		old_ids = self.activity_ids
   
		self.transaction do
			TakePart.destroy_all(:organization_id => self.id)
			new_ids.each do |activity_id|
				if activity_id == "9"
					self.take_parts.build({ :organization_id => self.id, :activity_id => activity_id, :description => @other_activity })
				else
					self.take_parts.build({ :organization_id => self.id, :activity_id => activity_id })
				end
			end
		end
	end
	
	def Organization.context_search(text_to_search)
	  return [] if text_to_search.empty?
	  
	  text_to_search = "%#{text_to_search}%"
	  
	  # Искать по полям организаци
	  a = Organization.find(:all,
	  :conditions => ["russian_title LIKE ?
	    OR foreign_title LIKE ?
	    OR phone LIKE ?
	    OR fax LIKE ?
	    OR email LIKE ?
	    OR url LIKE ?
	    OR icq LIKE ?
	    OR skype LIKE ?
	    OR russian_post_address LIKE ?
	    OR foreign_post_address LIKE ?
	    OR phone1 LIKE ?
	    OR phone2 LIKE ?
	    OR email1 LIKE ?
	    OR email2 LIKE ?",
	    text_to_search, 
	    text_to_search, 
	    text_to_search,
	    text_to_search, 
	    text_to_search, 
	    text_to_search,
	    text_to_search, 
	    text_to_search, 
	    text_to_search,
	    text_to_search, 
	    text_to_search, 
	    text_to_search,
	    text_to_search,
	    text_to_search])
	  
	  
	  # Искать по полям сотрудников
	  b = Person.find(:all, :conditions => ["lastname LIKE ?
	    OR firstname LIKE ?
	    OR middlename LIKE ?
	    OR post LIKE ?",
	    text_to_search,
	    text_to_search,
	    text_to_search,
	    text_to_search])
	    
	  c = []
	  for person in b
	    c << person.organization 
	  end
	    
	  # Возвратить объединение
	  return a | c
	end
	
	
	# validates_presence_of :russian_title, :message => "Русское название не должно быть пустым"
	validates_presence_of :place, :message => "Вы не указали страну юридической регистрации"	
	validates_format_of   :email, :email1, :email2, :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))*\Z/i, :message => "Неправильный формат эл. почты"
end